﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VK.Comm.Session;
using VK.Comm.Cookie;

namespace VK.Casemeet.Employees
{
    public static class EmployeeService
    {
        public static bool IsEmployee()
        {
            EmployeeEntity employee = SessionOperate<EmployeeEntity>.GetEntity(CS.AdminSessionKey);
            if (employee == null)
            {
                employee = CookieOperate<EmployeeEntity>.GetEntity(CS.AdminCookieKey);
            }
            if (employee != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool IsRights(int rightsID)
        {
            int count = EmployeeDAL.GetEmployeeRights(GetSessionEmployee().EmployeeID, rightsID, GetSessionEmployee().RoleID);
            if (count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool IsRightsGroup(int groupID)
        {
            int count = EmployeeDAL.GetEmployeeRightsGroup(GetSessionEmployee().EmployeeID, groupID, GetSessionEmployee().RoleID);
            if (count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool Enter(string name, string pass, bool isSave)
        {
            EmployeeEntity employee = EmployeeDAL.GetEmployee(name, pass);
            if (employee != null)
            {
                SessionOperate<EmployeeEntity>.SetEntity(CS.AdminSessionKey, employee);
                if (isSave)
                {
                    CookieOperate<EmployeeEntity>.SetEntity(CS.AdminCookieKey, employee, 30);
                }
                return true;
            }
            else
            {
                return false;
            }
        }

        public static void Leave()
        {
            SessionOperate<EmployeeEntity>.RemoveEntity(CS.AdminSessionKey);
            CookieOperate<EmployeeEntity>.RemoveEntity(CS.AdminCookieKey);
        }

        public static EmployeeEntity GetSessionEmployee()
        {
            EmployeeEntity employee = SessionOperate<EmployeeEntity>.GetEntity(CS.AdminSessionKey);
            if (employee == null)
            {
                employee = CookieOperate<EmployeeEntity>.GetEntity(CS.AdminCookieKey);
            }
            if (employee != null)
            {
                return employee;
            }
            else
            {
                System.Web.HttpContext.Current.Response.Redirect("/Admin/Default.aspx");
                return null;
            }
        }
    }
}
